feat(idempotency): add local cache to BasePersistenceLayer
#1396
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of your changes
This PR introduces a Least Recently Used (LRU) cache to the
BasePersistenceLayer
of the upcoming Idempotency utility. In this type of cache the most recently-used items are kept in the cache while older ones, that have been accessed less recently are evicted as new items come in.Users can configure any persistence layer that extends the
BasePersistenceLayer
to use this cache, and in doing so they can also specify the maximum amount of items that are kept in the cache:When caching is enabled, the persistence layer stores, retrieves, and deletes idempotency records from the cache.
Notes on the LRUCache implementation
As discussed in the linked issue, contrary to Python, Node.js doesn't have any LRU Cache implementation in its standard library. Prior to working the PR I have researched popular modules and found two:
lru-cache
(123M weekly downloads) andlru_map
(3.7M weekly downloads).While both libraries are popular, the
lru-cache
one offers a wide range of features like TTL, retrieval functions from remote, and a host of other features that we weren't going to be using in this utility. This, in addition to the module size (638 kB unpacked - vs ~150 kB on average on our side), made me opt for the less popular one.When looking at the
lru_map
module, I realized that the recommended usage stated this:For this reason I have opted for reimplementing the module directly in our repo. The resulting implementation follows loosely the one found in the
lru_map
module in terms of flow, but adds type annotations, comments, unit tests and retains only the bare minimum methods/features that are needed for Idempotency to work.The result of this work can be found at
packages/idempotency/src/persistence/LRUCache.ts
and the license as well as a direct mention to the original implementation and recommended usage are included in the docstring of the class.For now I have opted for putting the class in the Idempotency utility, however in the future we might consider extracting it in the
commons
package so that it can be used by other modules, as well as customers in their own code.Once merged this PR closes #1299
Related issues, RFCs
Issue number: #1299
Checklist
Breaking change checklist
Is it a breaking change?: NO
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.